{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "from pyreadstat import pyreadstat\n",
    "import scipy.stats as stats\n",
    "import statsmodels.api as sm\n",
    "import mytools"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "df, metadata = pyreadstat.read_sav(R'data\\indentity问卷数据数据清理后.sav',\n",
    "                                   apply_value_formats=True,\n",
    "                                   formats_as_ordered_category=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 卡方检验\n",
    "x = df['政治面貌']\n",
    "y = df['会打多少分']\n",
    "chi2, p, dof, ex = stats.chi2_contingency(pd.crosstab(x, y))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'接收虚无假设，拒绝研究假设。'"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "mytools.p_result(p)['conclusion']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "def chi_test(df,x,y):\n",
    "    \"\"\"卡方检验函数\"\"\"\n",
    "    x1 = df[x]\n",
    "    y1 = df[y]\n",
    "    chi2, p, dof, ex = stats.chi2_contingency(pd.crosstab(x1, y1))\n",
    "    return F'变量{x}与变量{y}的相关性检验结果为：卡方值为{chi2:.3f}, 自由度为{dof}，p值为{p:.3f}，{mytools.p_result(p)[\"conclusion\"]}'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'变量政治面貌与变量会打多少分的相关性检验结果为：卡方值为16.888, 自由度为12，p值为0.154，接收虚无假设，拒绝研究假设。'"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "chi_test(df,'政治面貌','会打多少分')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "SomersDResult(statistic=-0.027250783174111757, pvalue=0.3228126575133773, table=array([[  0,   0,   5,   7,   2],\n",
       "       [  5,   9,  37, 152,  82],\n",
       "       [ 11,  13,  63, 111,  66],\n",
       "       [  7,   3,  33,  86,  38],\n",
       "       [  2,   2,  16,  43,  30]]))"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = df['年级'].cat.codes\n",
    "y = df['会打多少分'].cat.codes\n",
    "stats.somersd(x, y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "x = '认知维度'\n",
    "y = '情感维度'\n",
    "r,p_r = stats.pearsonr(df[x], df[y])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'p': 'p=0.809>0.05', 'tex_p': 'p>0.05', 'conclusion': '接收虚无假设，拒绝研究假设。'}"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "mytools.p_result(p_r)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.8.10 64-bit",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.10"
  },
  "orig_nbformat": 4,
  "vscode": {
   "interpreter": {
    "hash": "9650cb4e16cdd4a8e8e2d128bf38d875813998db22a3c986335f89e0cb4d7bb2"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
